package course16;

import java.io.*;

public class FileInputStreamTest2 {

    public static void main(String[] args) throws Exception {
//从文件读取数据，用new实例化
        File file = new File("D:/mywork/README.md");
//如果没有就创建一个
        FileOutputStream fileoutputStream = new FileOutputStream(file);
//构建对象OutputStreamWriter，参数可以指定编码
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileoutputStream, "UTF-8");
//写入缓存
        outputStreamWriter.append("这是我的工程文件夹，里面存放了我的工程，每个文件夹是一个单独的工程");
        outputStreamWriter.append("\n");//换行
        outputStreamWriter.append("我再gitee上有他们的同名仓库，与他们一一对应，绑定");
        outputStreamWriter.append("\n");
        outputStreamWriter.append("使用gitee极大的提高了我学习编程的效率");

        outputStreamWriter.close();//关闭写入流
        fileoutputStream.close();//关闭输出流

        //在控制台读取文件内容
        //创建FileInputStream对象
        FileInputStream fileoutput = new FileInputStream(file);
//创建InputStreamReader对象，编码和写入的相同
        InputStreamReader reader = new InputStreamReader(fileoutput, "UTF-8");

        StringBuffer stringBuffer1 = new StringBuffer();

        while (reader.ready()) {
            //转成char加入到StringBuffer对象中
            stringBuffer1.append((char) reader.read());
        }
        System.out.println(stringBuffer1.toString());

        reader.close();//关闭读取流
        fileoutput.close();//关闭输出流
    }
}


